﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;

namespace backoffice
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           //datainit();
         //   tester();
        }
        public void tester()
        {
            backofficecomponent component = new backofficecomponent();
            component.CustomerPerCashBox = 200;
            component.MinKassa = 1;
            DataTable dt = new DataTable();
            dt = component.GetDateGroups();
            DataTable eachdate = new DataTable();
            eachdate = component.GetSingleDateGroup(dt.Rows[1]["grouped"].ToString());
            string dd = component.GoogleChartDraw(eachdate, 1, "contorid111", "one");
        }

        public void datainit()
        {
            backofficecomponent component = new backofficecomponent();
            component.CustomerPerCashBox = 200;
            component.MinKassa = 1;
            DataTable dt = new DataTable();
            dt = component.GetDateGroups();
            HtmlGenericControl[] control = new HtmlGenericControl[dt.Rows.Count];
            HtmlGenericControl test = new HtmlGenericControl("Script");
            test.Attributes.Add("type", "text/javascript");
            StringBuilder googlecallback = new StringBuilder();
            string s = "";
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                

               // control[i] =(HtmlGenericControl) component.HtmlControlGenerator("div", "ChartView_" + i.ToString());
               // googlecallback = component.GoogleCallbackFunctionGenerator(googlecallback, "drawChart", i);
                PlaceHolder1.Controls.Add(control[i]);
                DataTable eachdate = new DataTable();
                eachdate = component.GetSingleDateGroup(dt.Rows[i]["grouped"].ToString());
                StringBuilder Script = new StringBuilder();
                Script = component.GoogleChartDataTableInitializer(i);
             //   Script = component.GoogleChartTableColumneGenerator(Script, "string", "Tid");
               // Script = component.GoogleChartTableColumneGenerator(Script, "number", "Antal Kassa");
                Script = component.GoogleChartTableNumberOfRows(eachdate.Rows.Count);
                backofficereport[] report = backofficereport.InitArray(eachdate.Rows.Count);
                report = component.FillReporterArrayFromDataBaseTable(eachdate);
                component.SetInterval(report[1], report[2]);
                for (int p = 0; p < eachdate.Rows.Count; p++)
                {
        //            Script = component.GoogleChartTableAddRow(Script, p.ToString(), "0", report[p].TimeUnit.Hours.ToString(), true);
          //          Script = component.GoogleChartTableAddRow(Script, p.ToString(), "1", component.GetCashBoxPerInterval(report[p]).ToString(), false);
                }
                string de = DateTime.Parse(dt.Rows[i]["grouped"].ToString()).ToShortDateString();
               // Script = component.GoogleChartVisualizationChartObjectBuilder(Script, control[i].ID.ToString(), de , 600, 300);
                //Script = component.GoogleChartFinilazer(Script);
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", Script.ToString());
                s += Script.ToString();
                test.InnerText += googlecallback.ToString();
                
            }
            //foreach (HtmlGenericControl c in control)
            //{
            //    PlaceHolder1.Controls.Add(c);
            //}

            ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", s);
            PlaceHolder2.Controls.Add(test);
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
           
            
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            backofficecomponent component = new backofficecomponent();
            component.CustomerPerCashBox = Convert.ToInt16(TextBox1.Text);
            component.MinKassa = Convert.ToInt16(TextBox2.Text);
            DataTable dt = new DataTable();
            dt = component.GetDateGroups();
            HtmlGenericControl[] jsfunction = new HtmlGenericControl[dt.Rows.Count];
            HtmlGenericControl[] divtag = new HtmlGenericControl[dt.Rows.Count];
            HtmlGenericControl googlecallback = new HtmlGenericControl("script");
            googlecallback.Attributes.Add("type", "text/javascript");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsfunction[i] = new HtmlGenericControl("script");
                jsfunction[i].Attributes.Add("type", "text/javascript");
                divtag[i] = (HtmlGenericControl)component.HtmlControlGenerator("div", "draw_view", i);
                DataTable eachdate = new DataTable();
                eachdate = component.GetSingleDateGroup(dt.Rows[i]["grouped"].ToString());
                jsfunction[i].InnerText = component.GoogleChartDraw(eachdate, i, divtag[i].ID, DateTime.Parse(dt.Rows[i]["grouped"].ToString()).ToShortDateString());
                googlecallback.InnerText += component.GoogleCallbackFunctionGenerator("drawChart", i);
            }

            foreach (HtmlGenericControl c in jsfunction)
                PlaceHolder1.Controls.Add(c);
            foreach (HtmlGenericControl c in divtag)
                PlaceHolder2.Controls.Add(c);

            PlaceHolder3.Controls.Add(googlecallback);
        }
    
    }
}
